/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sevent.controller;

import com.sevent.service.EventService;
import com.sevent.util.Constants;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;

/**
 * 
 * @author enriquer
 */
@Controller()
public class EventController {

    @Autowired
    private EventService eventService;
    private static final Log log = LogFactory.getLog(EventController.class);

    @RequestMapping(value = "/newEvent.htm", method = RequestMethod.GET)
    public ModelAndView newEvent(HttpServletRequest request) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("newEvent::start");
            }

            //now Date and Time
            String date = DateFormatUtils.format(new java.util.Date(), Constants.DATE_FORMAT);
            String time = DateFormatUtils.format(new java.util.Date(), Constants.TIME_FORMAT);

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("date", date);
            params.put("time", time);

            return new ModelAndView("newEvent", "params", params);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("newEvent::finish");
            }
        }
    }

    @RequestMapping(value = "/lsEvents.htm", method = RequestMethod.GET)
    public @ResponseBody
    Map listEvents(HttpServletRequest request) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("listEvents::start");
            }

            //tmp
            int hostId = 1;
            Map searchParams = Collections.singletonMap("hostId", hostId);

            List<Map<String, Object>> lsEvents = eventService.findEvents(searchParams);

            Map<String, Object> response = new HashMap<String, Object>();
            response.put("identifier", "eventId");
            response.put("items", lsEvents);

            return response;
            //return new ModelAndView("findEvents", "listEvents", lsEvents);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("listEvents::finish");
            }
        }
    }

    @RequestMapping(value = "/myEvents.htm", method = RequestMethod.GET)
    public ModelAndView myEvents(HttpServletRequest request) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("myEvents::start");
            }

            return new ModelAndView("myEvents");
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("myEvents::finish");
            }
        }
    }

    @RequestMapping(value = "/findEvents.htm", method = RequestMethod.GET)
    public ModelAndView findEvents(HttpServletRequest request) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("findEvents::start");
            }

            return new ModelAndView("findEvents");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("findEvents::finish");
            }
        }
    }

    @RequestMapping(value = "/editEvent.htm", method = RequestMethod.GET)
    public ModelAndView editEvent(HttpServletRequest request) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("editEvent::start");
            }

            log.info("eventId: " + request.getParameter("eventId"));
            int eventId = Integer.parseInt(request.getParameter("eventId"));

            Map<String, Object> event = eventService.findEventById(eventId);

            Timestamp startDate = (Timestamp) event.get("startDate");
            Timestamp endDate = (Timestamp) event.get("endDate");

            event.put("startDate", DateFormatUtils.format(startDate, Constants.DATE_FORMAT));
            event.put("startTime", DateFormatUtils.format(startDate, Constants.TIME_FORMAT));
            event.put("endDate", DateFormatUtils.format(endDate, Constants.DATE_FORMAT));
            event.put("endTime", DateFormatUtils.format(endDate, Constants.TIME_FORMAT));

            return new ModelAndView("editEvent", "params", event);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("editEvent::finish");
            }
        }
    }

    @RequestMapping(value = "/menuEvents.htm", method = RequestMethod.GET)
    public ModelAndView menuEvents(HttpServletRequest request) {
        return new ModelAndView("menuEvents");
    }

    @RequestMapping(value = "/saveEvent.htm", method = RequestMethod.POST)
    public @ResponseBody Map saveEvent(HttpServletRequest request) {

        Map<String, Object> response = new HashMap<String, Object>();
        try {
            if (log.isDebugEnabled()) {
                log.debug("saveEvent::start");
            }
            Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
            if (log.isDebugEnabled()) {
                log.debug("request params: " + params);
            }

            Map<String, Object> eventData = new HashMap<String, Object>();

            String startDate = StringUtils.substring((String) params.get("txtStartTime"), 1, 6);
            startDate = params.get("txtStartDate") + " " + startDate;
            eventData.put("startDate", new Timestamp(DateUtils.parseDate(startDate, new String[]{Constants.FULL_DATE_FORMAT}).getTime()));

            String endDate = StringUtils.substring((String) params.get("txtEndTime"), 1, 6);
            endDate = params.get("txtEndDate") + " " + endDate;
            eventData.put("endDate", new Timestamp(DateUtils.parseDate(endDate, new String[]{Constants.FULL_DATE_FORMAT}).getTime()));

            eventData.put("name", MapUtils.getString(params, "txtName"));
            eventData.put("eventId", MapUtils.getInteger(params, "txtEventId"));
            eventData.put("description", MapUtils.getString(params, "txtDescription"));
            eventData.put("hostId", 1);///temp
            eventData.put("typeId", "01");///temp
            eventData.put("privacy", MapUtils.getString(params, "cbxPrivacy"));
            eventData.put("status", MapUtils.getString(params, "txtStatus"));
            eventData.put("limit", MapUtils.getInteger(params, "txtLimit"));

            Integer eventId = eventService.saveEvent(eventData);

            response.put("success", "1");
            //return new ModelAndView(new RedirectView("editEvent.htm?eventId=" + eventId), null);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("saveEvent::finish");
            }
        }
        return response;
    }
}
